package com.earnings.okhttputils.utils.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.utils.CommonUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;


/*
选择对话框
 */
public class ShareDialog extends Dialog implements
		View.OnClickListener {
	private Activity act;
	private LinearLayout mView;
	private String sex;

	/**
	 * 初始化
	 *
	 * @param context
	 */
	public ShareDialog(Context context) {
		super(context, R.style.dialog_msg);
		this.act = (Activity) context;

		mView = (LinearLayout) act.getLayoutInflater().inflate(R.layout.dialog_share,
				null);/*
		layout= (LinearLayout) mView.findViewById(R.id.cont);
		layout.getLayoutParams().width = act.getWindowManager()
				.getDefaultDisplay().getWidth();
*/
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		/*((TextView)mView.findViewById(R.id.text)).setText(text);
		mView.findViewById(R.id.cencel).setOnClickListener(this);
		mView.findViewById(R.id.sumbit).setOnClickListener(this);
		*/// setCanceledOnTouchOutside(false);
		// setCancelable(false);
		((TextView)mView.findViewById( R.id.name )).setText( CommonUtil.getUser().getNickname() );
		Glide.with(act).load( CommonUtil.getUser().getHead_pic() ).into( (ImageView)mView.findViewById( R.id.image ));
		 init();
	}

	private String title="招财汇宝";
	private String cont="";
	private String url="";
	private UMImage image;

	public String getTitle() {

		return title;
	}
	public void setUrl(String url) {
		web=new UMWeb(url);
		web.setTitle(title);
		web.setDescription(cont);
		web.setThumb(image);
	}
	public void setTitle(String title) {
		this.title = title;
		web.setTitle(title);
	}

	public String getCont() {
		return cont;
	}
	private boolean isImage=false;
	public void setImageOnly(Bitmap bitmap){
		isImage=true;
		image=new UMImage(act,bitmap);
	}
	public void setCont(String cont) {
		this.cont = cont;
		web.setDescription(cont);
	}

	public String getUrl() {
		return url;
	}
	UMWeb web;
	public void init() {
		//share=new ShareUtil(act,url);

		web = new UMWeb( "http://www.baidu.com" );
		///web.setThumb(thumb);
		image = new UMImage( act, R.drawable.ic_launcher);
		web.setDescription( cont );
		web.setTitle( title );
		web.setThumb( image );

		final View.OnClickListener listener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {


				int i = v.getId();
				if (i == R.id.btn_wx) {//Toast.makeText(act, " 分享成功啦", Toast.LENGTH_SHORT).show();
					if (isImage)
						new ShareAction( act ).withMedia( image ).setPlatform( SHARE_MEDIA.WEIXIN ).setCallback( umShareListener ).share();
					else
						new ShareAction( act ).withMedia( web ).setPlatform( SHARE_MEDIA.WEIXIN ).setCallback( umShareListener ).share();


				} else if (i == R.id.btn_wxfriend) {
					if (isImage)
						new ShareAction( act ).withMedia( image ).setPlatform( SHARE_MEDIA.WEIXIN_CIRCLE ).setCallback( umShareListener ).share();
					else
						new ShareAction( act ).withMedia( web ).setPlatform( SHARE_MEDIA.WEIXIN_CIRCLE ).setCallback( umShareListener ).share();


				} else if (i == R.id.btn_qqzone) {
					if (isImage)
						new ShareAction( act ).withMedia( image ).setPlatform( SHARE_MEDIA.QZONE ).setCallback( umShareListener ).share();
					else
						new ShareAction( act ).withMedia( web ).setPlatform( SHARE_MEDIA.QZONE ).setCallback( umShareListener ).share();


				} else if (i == R.id.btn_qq) {
					if (isImage)
						new ShareAction( act ).withMedia( image ).setPlatform( SHARE_MEDIA.QQ ).setCallback( umShareListener ).share();
					else
						new ShareAction( act ).withMedia( web ).setPlatform( SHARE_MEDIA.QQ ).setCallback( umShareListener ).share();


				}

				dismiss();
			}
		};

		mView.findViewById(R.id.btn_wxfriend).setOnClickListener(listener);
		mView.findViewById(R.id.btn_wx).setOnClickListener(listener);
		mView.findViewById(R.id.btn_qq).setOnClickListener(listener);
		mView.findViewById(R.id.btn_qqzone).setOnClickListener(listener);
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(layout);
		this.setCancelable(true);// 可以用“返回键”取消
		Window window = this.getWindow();
		window.setContentView(mView);
		window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
	}

	@Override
	public void dismiss() {
		// TODO 自动生成的方法存根
		super.dismiss();
		// TextView tv=(TextView) act.findViewById(R.id.province);
		// tv.setText(province);
	}

	@Override
	public void onClick(View v) {
		dismiss();
	}
	private UMShareListener umShareListener = new UMShareListener() {
		@Override
		public void onStart(SHARE_MEDIA share_media) {

		}

		@Override
		public void onResult(SHARE_MEDIA platform) {
			Toast.makeText(act, " 分享成功", Toast.LENGTH_SHORT).show();

		}

		@Override
		public void onError(SHARE_MEDIA platform, Throwable t) {

			Toast.makeText(act," 分享失败", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onCancel(SHARE_MEDIA platform) {
			Toast.makeText(act," 分享取消", Toast.LENGTH_SHORT).show();
		}
	};
}
